
// js  js



//根据学号查信息
var searchInput = document.getElementById('searchInput');
var searchBtn = document.getElementById('searchBtn');
var twiceTd = document.getElementsByClassName('twiceTd');
var thisImg = document.getElementById('thisImg');
var elseImg = document.getElementById('elseImg');
searchBtn.onclick = ()=>{
    if(searchInput.value){
        let value = searchInput.value;
        axios.get(`/admin/upload/getInfoByStuId?stuId=${value}`)
        .then((data)=>{
            let info = data.data.data.object;
            console.log(info)
            if(info){
                twiceTd[0].innerHTML = info.name;
                twiceTd[1].innerHTML = info.studentId;
                if(info.firstPath !== "未上传简历"){
                    thisImg.setAttribute('src',info.firstPath)
                }
                if(info.nextPath !== "未上传简历"){
                    elseImg.setAttribute('src',info.nextPath)
                }
                
            }else{
                // alert('学号有误')
                layui.use("layer",function(){
                    layer.msg('请输入正确学号或姓名！', {
                        icon: 7,
                        time: 2000
                    })
                }) 
                // searchInput.value=' '   
            }
        })
        .catch((err)=>{
            layui.use("layer",function(){
                layer.msg('请输入正确学号或姓名！', {
                    icon: 7,
                    time: 2000
                })
            }) 
            console.log(err)
        })
    }else{
        // alert('请输入查找内容')
        layui.use("layer",function(){
            layer.msg('请输入查找内容！', {
                icon: 7,
                time: 2000
            })
        })    
    }
}



function jieliu(fn,wait){
    let preTime = Date.now();
    return function(){
        var nowTime = Date.now();
        var time = nowTime - preTime;
        if(time > wait){
            fn();
        }else{
            return false;
        }
        preTime = Date.now();
    }
}
var mySearchTable = document.getElementsByClassName('mySearchTable')[0];
var tbody = document.getElementById('tbody');
function showStu(){
    if(searchInput.value){
        //输入的第一个数字不是数字
        if(isNaN(Number(searchInput.value[0]))){
            let name = searchInput.value;
            axios.get(`/admin/upload/getInfoByName?name=${name}`)
            .then((data)=>{
                let info = data.data.data;
                if(info.length !== 0){
                    let str = `<tr>
    <td style="font-weight: 600;">姓名</td>
    <td style="font-weight: 600;">学号</td>
</tr>`;
                    for(aa of info){
                        str += `
<tr>
    <td stuId=${aa.studentId} class="td">${aa.name}</td>
    <td stuId=${aa.studentId} class="td">${aa.studentId}</td>
</tr>`
                    }
                    mySearchTable.style.display = 'table';
                    tbody.innerHTML = str;
                }else{
                    //如果没有数据
                    mySearchTable.style.display = 'none';
                    tbody.innerHTML = "";
                }
                console.log(info)
            })
            .catch((err)=>{
                console.log(err)
            })
        }else{
            
        }
    }else{
        //如果输入框里面没有内容
        mySearchTable.style.display = 'none';
        tbody.innerHTML = "";
        return false;
    }
}
searchInput.oninput = jieliu(showStu,100)

//点击搜索出来的饿内容就直接把这个学生的信息放上去了
$('.mySearchTable').on('click','.td',(ev)=>{
    let target = ev.target || ev.srcElement;
    axios.get(`/admin/upload/getInfoByStuId?stuId=${target.getAttribute('stuid')}`)
    .then((data)=>{
        let info = data.data.data.object;
        console.log(info)
        if(info){
            twiceTd[0].innerHTML = info.name;
            twiceTd[1].innerHTML = info.studentId;
            if(info.firstPath !== "未上传简历"){
                thisImg.setAttribute('src',info.firstPath)
            }else{
                thisImg.removeAttribute('src')

            }
            if(info.nextPath !== "未上传简历"){
                elseImg.setAttribute('src',info.nextPath)
            }else{
                elseImg.removeAttribute('src')
            }
            mySearchTable.style.display = 'none';
            tbody.innerHTML = "";
            searchInput.value = "";
        }
    })
    .catch((err)=>{
        console.log(err)
    })
})

//上传简历正面
var firstImgInput = document.getElementById('firstImgInput');
firstImgInput.onclick = ()=>{
    if(twiceTd[1].innerHTML){

    }else{
        // alert('请指定学号')
        layui.use("layer",function(){
            layer.msg('请指定学号或姓名！', {
                icon: 7,
                time: 2000
            })
        })    
        return false
    }
}
firstImgInput.onchange = ()=>{
    let formData = new FormData();
    formData.append('file',firstImgInput.files[0]);
    formData.append('studentId',twiceTd[1].innerHTML)
    formData.append('path',0)
    let config = {
        headers: {
            'Content-Type':'multipart/form-data',
            
        }
    }
    axios.post('/admin/upload/uploadThis',formData,config)
    .then((data)=>{
        console.log(data)
        thisImg.setAttribute('src',data.data.data.object);
        // alert('上传成功')
        layui.use("layer",function(){
            layer.msg('上传成功！', {
                icon: 1,
                time: 2000
            })
        })    
        firstImgInput.value = '';
    })  
    .catch((err)=>{
        console.log(err)
    })

}

//上传简历背面
var secondImgInput = document.getElementById('secondImgInput');
secondImgInput.onclick = (ev)=>{
    if(twiceTd[1].innerHTML){

    }else{
        // alert('请指定学号')
        layui.use("layer",function(){
            layer.msg('请指定学号或姓名！', {
                icon: 7,
                time: 2000
            })
        })    
        return false
    }
}
secondImgInput.onchange = ()=>{
    let formData = new FormData();
    console.log(secondImgInput.files[0])
    formData.append('file',secondImgInput.files[0]);
    formData.append('studentId',twiceTd[1].innerHTML)
    formData.append('path',1)
    console.log(formData)
    let config = {
        headers: {
            'Content-Type':'multipart/form-data',
        }
    }
    axios.post('/admin/upload/uploadThis',formData,config)
    .then((data)=>{
        elseImg.setAttribute('src',data.data.data.object)
        // alert('上传成功')
        layui.use("layer",function(){
            layer.msg('上传成功！', {
                icon: 1,
                time: 2000
            })
        })    
        secondImgInput.vlue = '';
    })
    .catch((err)=>{
        console.log(err)
    })

}

// 打开二维码
var openCode = document.getElementById('openCode');
var codeImg = document.getElementById('codeImg');
openCode.onclick =()=>{
    codeImg.style.display = 'block';
}

//关闭二维码
var closeCode = document.getElementById('closeCode');
closeCode.onclick = ()=>{
    codeImg.style.display = 'none';
}
var thisImg = document.getElementById('thisImg');
var openImg = document.getElementById('openImg');
var openThis = document.getElementById('openThis');
var closeImg = document.getElementById('closeImg');
var elseImg = document.getElementById('elseImg');


thisImg.onclick = (ev)=>{
    let target = ev.target || ev.srcElement;
    openImg.style.display = 'block';
    openThis.setAttribute('src',target.getAttribute('src'));
};
elseImg.onclick = (ev)=>{
    let target = ev.target || ev.srcElement;
    openImg.style.display = 'block';
    openThis.setAttribute('src',target.getAttribute('src'));
};
closeImg.onclick = ()=>{
    openImg.style.display = 'none';
    openThis.setAttribute('src',"");
}